package com.firestone.service.area.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.firestone.bean.dto.area.AreaDTO;
import com.firestone.bean.dto.area.SearchDTO;
import com.firestone.bean.entity.elasticsearch.im.DmImProject;
import com.firestone.bean.entity.mysql.primary.ImArea;
import com.firestone.core.constants.ConstantDmImProjectKey;
import com.firestone.core.constants.ConstantDmKey;
import com.firestone.dao.primary.area.ImAreaDao;
import com.firestone.service.area.IImAreaService;
import com.firestone.service.elasticsearch.IMyElasticsearchService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author CAIYJ
 * @since 2018-04-19
 */
@Service
public class ImAreaServiceImpl extends ServiceImpl<ImAreaDao, ImArea>
        implements IImAreaService {

    @Autowired
    private IMyElasticsearchService myElasticsearchService;

    @Override
    public Page<AreaDTO> listArea(Page<AreaDTO> page, SearchDTO dto)
            throws Exception {
        EntityWrapper<AreaDTO> ew = new EntityWrapper<>();
        ew.setEntity(new AreaDTO());
        if (null != dto) {
            ew.where("1 = {0}", 1);
            ew.and(StringUtils.isNotBlank(dto.getName()),
                    "ia.`name` LIKE concat('%',{0},'%')", dto.getName());
            ew.and(StringUtils.isNotBlank(dto.getArea()),
                    "ia.`area` LIKE concat('%',{0},'%')", dto.getArea());
            ew.and(StringUtils.isNotBlank(dto.getCity()),
                    "ia.`city` LIKE concat('%',{0},'%')", dto.getCity());
            ew.and(StringUtils.isNotBlank(dto.getProvince()),
                    "ia.`province` LIKE concat('%',{0},'%')",
                    dto.getProvince());
        }
        ew.orderBy("ia.update_time", false);
        List<AreaDTO> list = this.baseMapper.selectAreaList(page, ew);
        if (null != list && list.size() > 0) {
            Set<Long> ids = new HashSet<>();
            for (AreaDTO area : list) {
                ids.add(area.getId());
            }
            NestedQueryBuilder query = QueryBuilders
                    .nestedQuery(ConstantDmKey.category,
                            QueryBuilders.termsQuery(
                                    ConstantDmKey.category_mapId, ids),
                    ScoreMode.Total);
            Map<String, Long> aggs = myElasticsearchService.nestedAggs(query,
                    "category", ConstantDmImProjectKey.category, "mapId",
                    ConstantDmImProjectKey.category_mapId, DmImProject.class);
            for (AreaDTO area : list) {
                area.setCompanyNum(aggs.get(area.getId()) != null
                        ? aggs.get(area.getId()) : 0);
            }
        }
        return page.setRecords(list);
    }

}
